package com.whenjun.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ifootbook.util.EmptyUtil;
import com.ifootbook.util.FootBookException;
import com.whenjun.dto.base.BaseId;
import com.whenjun.dto.kq.KqLocationAddReq;
import com.whenjun.dto.kq.KqLocationDTO;
import com.whenjun.dto.kq.KqLocationReq;
import com.whenjun.dto.kq.KqLocationUpdReq;
import com.whenjun.dto.person.IscPersonDTO;
import com.whenjun.entity.IscPerson;
import com.whenjun.entity.KqLocation;
import com.whenjun.mapper.KqLocationMapper;
import com.whenjun.service.IKqLocationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whenjun.util.IPageUtil;
import com.whenjun.util.SaTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whenjun
 * @since 2024-02-21
 */
@Service
public class KqLocationServiceImpl extends ServiceImpl<KqLocationMapper, KqLocation> implements IKqLocationService {

    @Autowired
    private KqLocationGroupServiceImpl kqLocationGroupService;

    @Transactional
    public IPage<KqLocationDTO> KqLocations(KqLocationReq kqLocationReq) {
        Page<KqLocation> page = new Page<>(kqLocationReq.getPage(), kqLocationReq.getSize());

        IPage<KqLocation> kqLocationIPage = this.lambdaQuery()
                .like(!EmptyUtil.isEmpty(kqLocationReq.getName()), KqLocation::getName, kqLocationReq.getName())
                .eq(KqLocation::getState, false)
                .orderByDesc(KqLocation::getCreateTime)
                .page(page)
                ;

        IPage<KqLocationDTO> kqLocationDTOIPage = IPageUtil.trans(kqLocationIPage);

        return kqLocationDTOIPage;
    }

    @Transactional
    public void KqLocationAdd(KqLocationAddReq kqLocationAddReq) {
        KqLocation kqLocation = this.lambdaQuery().eq(KqLocation::getName, kqLocationAddReq.getName()).one();
        if (kqLocation != null) {
            throw new FootBookException("name已存在，请修改后再试");
        }
        LocalDateTime now = LocalDateTime.now();
        String userId = SaTokenUtil.getUserId();

        kqLocation = new KqLocation();
        kqLocation.setName(kqLocationAddReq.getName());
        kqLocation.setState(false);
        kqLocation.setCreateUser(userId);
        kqLocation.setCreateTime(now);

        this.save(kqLocation);
    }

    @Transactional
    public void KqLocationDel(BaseId baseId) {
        KqLocation kqLocation = this.getById(baseId.getId());
        if (kqLocation == null) {
            throw new FootBookException("id错误");
        }
        LocalDateTime now = LocalDateTime.now();
        String userId = SaTokenUtil.getUserId();

        kqLocation.setState(true);
        kqLocation.setUpdateUser(userId);
        kqLocation.setUpdateTime(now);

//        this.updateById(kqLocation);
        this.removeById(kqLocation);

        kqLocationGroupService.KqLocationGroupDelByLocationId(kqLocation.getId());
    }

    @Transactional
    public void KqLocationUpd(KqLocationUpdReq kqLocationUpdReq) {
        KqLocation kqLocation = this.getById(kqLocationUpdReq.getId());
        if (kqLocation == null) {
            throw new FootBookException("id错误");
        }
        LocalDateTime now = LocalDateTime.now();
        String userId = SaTokenUtil.getUserId();

        kqLocation.setUpdateUser(userId);
        kqLocation.setUpdateTime(now);
        kqLocation.setName(kqLocationUpdReq.getName());

        this.updateById(kqLocation);
    }
}
